package edu.krazy.server;

import java.util.ArrayList;
import java.util.HashMap;
import edu.krazy.common.MessageType;
import edu.krazy.server.threads.AcceptClientThread;

public class KrazyServer implements IOnClientMessage {
	
	private HashMap<String, KrazyGame> mGames;
	private HashMap<String, KrazyGame> mLobbies;
	private ArrayList<KrazyClient> mClients;
	
	private int mPort;
	private AcceptClientThread mMainThread;
	
	public KrazyServer(int port){
		mPort = port;
		mClients = new ArrayList<KrazyClient>();
		mGames = new HashMap<String,KrazyGame>();
		mLobbies = new HashMap<String,KrazyGame>();
	}
	
	public void startServer(){
		mMainThread = new AcceptClientThread(this, mPort);
		mMainThread.start();
	}
	
	public void addClient(KrazyClient client){
		synchronized (mClients) {
			mClients.add(client);
			client.setListener(this);
			client.writeToClient(MessageType.GAME_LIST, getGamesStringList());
		}
	}
	
	private String getGamesStringList(){
		StringBuilder sb = new StringBuilder();
		for (String key : mLobbies.keySet()) {
			if(sb.length()>0){
				sb.append(",");
			}
			sb.append(key);
			
		}
		return sb.toString();
	}

	@Override
	public void broadcastObject(KrazyClient from, int msgType, Object object) {
		
		
	}
	
	@Override
	public void broadcastString(KrazyClient from, int msgType, String object) {
		int len = mClients.size();
		for (int i = 0; i < len; i++) {
			KrazyClient clnt;
			if(from != (clnt = mClients.get(i))){
				clnt.writeToClient(msgType, object);
			}
		}
		
	}

	@Override
	public void changeGameState(KrazyClient admin, int newState) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void createLobby(KrazyClient creator, String gameName) {
		//System.out.println("createLobby");
		if(mLobbies.containsKey(gameName) || mGames.containsKey(gameName)){
			creator.writeToClient(MessageType.GAME_ALREADY_EXISTS);
		}else{
			
			mClients.remove(creator);
			KrazyGame game = new KrazyGame(gameName, this);
			creator.writeToClient(MessageType.GAME_CREATED);
			game.addClient(creator);
			mLobbies.put(gameName, game);			
			broadcastString(creator, MessageType.GAME_LIST, getGamesStringList());
		}
		
	}

	@Override
	public void startGame() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void joinGame(KrazyClient from, String gameName) {
		if(mLobbies.containsKey(gameName)){
			mClients.remove(from);
			mLobbies.get(gameName).addClient(from);
		}
		
	}

	@Override
	public void broadcastMessageType(KrazyClient from, int msgType) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void readyGame() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void readyPlayer(KrazyClient client) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void leaveGame(KrazyClient client) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void clientDisconnected(KrazyClient client) {
		synchronized (mClients) {
			mClients.remove(client);
		}
		
	}

	@Override
	public void destroyGame(KrazyGame game) {
		if(mLobbies.containsKey(game.getName())){
			synchronized (mLobbies) {
				mLobbies.remove(game.getName());
			}
			broadcastString(null, MessageType.GAME_LIST, getGamesStringList());			
		}else if(mGames.containsKey(game.getName())){
			synchronized (mGames) {
				mGames.remove(game.getName());
			}
			broadcastString(null, MessageType.GAME_LIST, getGamesStringList());
		}
		
	}
}
